package net.kehuilai.tools;

import org.apache.poi.hssf.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * Created by genghz on 18/4/10.
 */
public class ExcelTool {

    private static final Logger log = LoggerFactory.getLogger(ExcelExportServiceImpl.class);

    private static final AWSCredentials credentials = new ProfileCredentialsProvider().getCredentials();
    private static final AmazonS3Client s3 = new AmazonS3Client(credentials);

    static {
        s3.setRegion(Region.getRegion(Regions.CN_NORTH_1));
    }

    public static String export(String group, long posWid, String name, List<String> header, List<List<String>> data) {

        HSSFWorkbook workbook = new HSSFWorkbook();

        HSSFSheet sheet = workbook.createSheet();

        // header 单元格样式
        HSSFCellStyle cellStyleHead = workbook.createCellStyle();
        HSSFFont font1 = workbook.createFont();
        font1.setFontHeightInPoints((short) 16);
        font1.setBold(true);
        cellStyleHead.setFont(font1);
        cellStyleHead.setFillBackgroundColor(HSSFColor.HSSFColorPredefined.GREY_25_PERCENT.getIndex());
        cellStyleHead.setFillPattern(FillPatternType.NO_FILL);

        // data 单元格样式
        HSSFCellStyle cellStyleContent = workbook.createCellStyle();
        HSSFFont font2 = workbook.createFont();
        font2.setFontHeightInPoints((short) 14);
        font2.setBold(false);
        cellStyleContent.setFont(font2);

        // 写入header
        HSSFRow row = sheet.createRow(0);
        for (int i = 0; i < header.size(); i++) {
            HSSFCell cell = row.createCell(i);
            cell.setCellStyle(cellStyleHead);
            cell.setCellValue(new HSSFRichTextString(header.get(i)));
        }

        // 写入表内容
        for (int i = 0; i < data.size(); i++) {
            row = sheet.createRow(i + 1);
            for (int j = 0; j < data.get(i).size(); j++) {
                HSSFCell cell = row.createCell(j);
                cell.setCellStyle(cellStyleContent);
                cell.setCellValue(new HSSFRichTextString(data.get(i).get(j)));
            }
        }

//        for (int i = 0; i <= header.size(); i++) {
//            sheet.autoSizeColumn(i);
//        }

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            workbook.write(baos);
        } catch (IOException e) {
            log.error("workbook write error");
            return null;
        } finally {
            try {
                baos.close();
            } catch (IOException e) {
                return null;
            }
        }

        byte[] xls = baos.toByteArray();
        return uploadToS3(group, posWid, name, xls);
    }

    private String uploadToS3(String group, long wid, String name, byte[] context) {
        String pathDate = DateTool.formatTime("yyyyMMdd");
        String bucket = "img.kehuilai.net";
        String path = StringTool.formatString("report/{}/{}/{}/{}.xls", group.info(), wid, pathDate, name);

        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentLength((long) context.length);
        s3.putObject(bucket, path, new ByteArrayInputStream(context), metadata);

        return StringTool.formatString("http://{}/{}", bucket, path);
    }
}
